/*
 * $Id: TcpAddress.cpp 50 2009-02-10 05:48:05Z hyunghwan.chung $
 *
   Copyright 2005-2009 Chung, Hyung-Hwan.

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an  BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */

#include <xp/net/TcpAddress.hpp>
#include <xp/net/Socket.hpp>

namespace xp
{
	namespace net
	{

		TcpAddress::TcpAddress (): InetAddress (V0)
		{
		}

		TcpAddress::TcpAddress (int family): InetAddress (family)
		{
		}

		TcpAddress::TcpAddress (const InetAddress& adr): InetAddress (adr)
		{
		}

		TcpAddress::TcpAddress (
			const struct sockaddr_in& adr): InetAddress (adr)
		{
		}

		TcpAddress::TcpAddress (
			const struct sockaddr_in6& adr): InetAddress (adr)
		{
		}

		TcpAddress& TcpAddress::operator= (const InetAddress& adr)
		{
			InetAddress::operator= (adr);
			return *this;
		}

		TcpAddress& TcpAddress::operator= (
			const struct sockaddr_storage& adr)
		{
			InetAddress::operator= (adr);
			return *this;
		}

		TcpAddress& TcpAddress::operator= (
			const struct sockaddr_in& adr)
		{
			InetAddress::operator= (adr);
			return *this;
		}

		TcpAddress& TcpAddress::operator= (
			const struct sockaddr_in6& adr)
		{
			InetAddress::operator= (adr);
			return *this;
		}

		xp_uint16_t TcpAddress::port () const
		{
			switch (this->family())
			{
				case V4:
				{
					struct sockaddr_in* p;
					p = (struct sockaddr_in*)this->storage();
					return p->sin_port;
				}

				case V6:
				{
					struct sockaddr_in6* p;
					p = (struct sockaddr_in6*)this->storage();
					return p->sin6_port;
				}

				default:
					return 0;
			}
		}

		void TcpAddress::setPort (xp_uint16_t v) 
		{
			switch (this->family())
			{
				case V4:
				{
					struct sockaddr_in* p;
					p = (struct sockaddr_in*)this->storage();
					p->sin_port = v;
					break;
				}

				case V6:
				{
					struct sockaddr_in6* p;
					p = (struct sockaddr_in6*)this->storage();
					p->sin6_port = v;
					break;
				}

				default:
					// do nothing
					break;
			}
		}
	}
}
